/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fw.anotations;

import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.logging.Level;
import java.util.logging.Logger;
import javassist.ClassPool;
import javassist.CtClass;
import javassist.Loader;
import javassist.NotFoundException;

/**
 *
 * @author HP_Propietario
 */
public class AnnotationsUtil {

    public static ClassLoader getClassLoader() {
        ClassLoader classLoader = null;
        try {

            classLoader = ClassLoader.getSystemClassLoader();
            classLoader.loadClass("testes.MiBean");

        } catch (ClassNotFoundException ex) {
            Logger.getLogger(AnnotationsUtil.class.getName()).log(Level.SEVERE, null, ex);
        }
        return classLoader;
    }

    @SuppressWarnings("unchecked")
    public static ClassAnnotationInfo getClassAnnotation(Class clazz) throws NotFoundException, ClassNotFoundException {
        ClassAnnotationInfo classAnnotationInfo = null;
        ClassPool pool = new ClassPool();

        //si  no,  no carga
        pool.appendSystemPath();
        Loader loader = new Loader(pool);
        Class class1 = loader.loadClass("testes.MiBean");
        CtClass ctClass = pool.get(class1.getName());

        Object[] objects = ctClass.getAnnotations();
        classAnnotationInfo = new ClassAnnotationInfo();
        for (int i = 0; i < objects.length; i++) {
            Annotation annotation = (Annotation) objects[i];
            Method[] methods = annotation.annotationType().getDeclaredMethods();
            for (int j = 0; j < methods.length; j++) {
                try {



                    Method method = methods[j];
                    String name = method.getName();
                    System.out.println("getClassAnnotation(Class clazz).name\t" + method.getName());
                    Object[] objects1 = new Object[]{null};
                    String value = (String) method.invoke(annotation, null);
                    classAnnotationInfo.getAnnotation().put(name, new AnotacionAtom(name, value));
                    System.out.println("getClassAnnotation(Class clazz).value\t" + value);



                } catch (IllegalAccessException ex) {
                    Logger.getLogger(AnnotationsUtil.class.getName()).log(Level.SEVERE, null, ex);
                } catch (IllegalArgumentException ex) {
                    Logger.getLogger(AnnotationsUtil.class.getName()).log(Level.SEVERE, null, ex);
                } catch (InvocationTargetException ex) {
                    Logger.getLogger(AnnotationsUtil.class.getName()).log(Level.SEVERE, null, ex);
                }


            }

        }
        return classAnnotationInfo;
    }
}
